<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        var data = [
            {
                id: 1,
                name: '家电',
                goods: [{
                    id: 11,
                    gname: '冰箱',
                    goods: [{
                        id: 21,
                        gname: '海尔冰箱'
                    },
                    {
                        id: 22,
                        gname: '美的冰箱'
                    },
                    {
                        id: 23,
                        gname: '戴尔冰箱'
                    }]
                }, {
                    id: 12,
                    gname: '洗衣机'
                }]
            },
            {
                id: 2,
                name: '服饰',
            }
        ]
        var need_data = {} // 最终需要id为22的对象

        function filter_good(data, id) {
            data.forEach(item => {
                if (item.id === id) {
                    need_data = item
                } else if (item.goods && item.goods.length) {
                    filter_good(item.goods, id)
                }
            })
        }
        
        filter_good(data, 22)
        console.log(need_data)
    </script>
</head>

<body>

</body>

</html>